package com.rambo.androidnews.fraction;

import com.rambo.androidnews.ListTabBean;
import com.rambo.androidnews.ResourceTable;
import com.rambo.androidnews.fraction.projectcase.WanAdProjectCaseFraction;
import com.rambo.androidnews.itemprovider.WanAdMainTabProvider;
import com.rambo.baselib.fraction.BaseFraction;
import com.rambo.baselib.mvp.BasePresenter;
import com.rambo.baselib.util.FractionUtils;
import com.rambo.baselib.util.ToastUtil;
import ohos.aafwk.ability.AbilityForm;
import ohos.aafwk.ability.OnClickListener;
import ohos.aafwk.ability.ViewsStatus;
import ohos.aafwk.ability.fraction.Fraction;
import ohos.aafwk.ability.fraction.FractionManager;
import ohos.agp.components.Component;
import ohos.agp.components.ListContainer;
import ohos.agp.components.TabList;
import ohos.app.Context;

import java.util.ArrayList;
import java.util.List;


/***********************************************************************
 * <p>@description: 玩Android 网站
 * <p>@author: rambo
 * <p>@version: 1.0.0
 * <p>@created on: 2022/1/13 16:07
 **********************************************************************/
public class WanAdMainFraction extends BaseFraction {
    private TabList listTab;
    private Fraction curFraction;

    public WanAdMainFraction(Context context) {
        super(context);
    }

    @Override
    protected int getLayoutResId() {
        return ResourceTable.Layout_fraction_wan_android;
    }

    @Override
    protected void initView(Component component) {
        super.initView(component);
        listTab = (TabList) component.findComponentById(ResourceTable.Id_list_tab);

        createTab();


    }

    private void createTab() {
        TabList.Tab tab0 = listTab.new Tab(this);
        tab0.setText("首页");
        listTab.addTab(tab0);

        TabList.Tab tab1 = listTab.new Tab(this);
        tab1.setText("问答");
        listTab.addTab(tab1);

        TabList.Tab tab2 = listTab.new Tab(this);
        tab2.setText("项目");
        listTab.addTab(tab2);

        TabList.Tab tab3 = listTab.new Tab(this);
        tab3.setText("公众号");
        listTab.addTab(tab3);

        TabList.Tab tab4 = listTab.new Tab(this);
        tab4.setText("分类");
        listTab.addTab(tab4);
        listTab.selectTabAt(0);
        Fraction fraction = new WanAdHomeFraction(context);
        listTab.getTabAt(0).setTag(fraction);
        FractionUtils.show(getFractionAbility().getFractionManager(),
                ResourceTable.Id_article_container, curFraction, fraction);
        curFraction = fraction;
    }

    @Override
    protected void registerClick() {
        super.registerClick();
        listTab.addTabSelectedListener(new TabList.TabSelectedListener() {
            @Override
            public void onSelected(TabList.Tab tab) {
                Object tag = tab.getTag();
                if (curFraction != null && tag == curFraction) {
                    return;
                }
                int position = listTab.getSelectedTabIndex();
                Fraction fraction = null;
                if (tag == null) {
                    if (position == 0) {
                        fraction = new WanAdHomeFraction(context);
                    } else if (position == 1) {
                        fraction = new WanAdQaFraction(context);
                    } else if (position == 2) {
                        fraction = new WanAdProjectFraction(context,294);
                    } else if (position == 3) {
                        fraction = new WanAdWxArticleFraction(context);
                    } else if (position == 4) {
                        fraction = new WanAdProjectCaseFraction(context);
                    }
                    tab.setTag(fraction);
                }else {
                    fraction=(Fraction)tag ;
                }
                FractionUtils.show(getFractionAbility().getFractionManager(),
                        ResourceTable.Id_article_container, curFraction, fraction);
                curFraction = fraction;
            }

            @Override
            public void onUnselected(TabList.Tab tab) {

            }

            @Override
            public void onReselected(TabList.Tab tab) {

            }
        });
    }

    @Override
    protected BasePresenter createPresenter() {
        return null;
    }
}
